package com.atguigu.udp;
import java.io.*;
import java.net.*;
public class UDP_Send {
    public static void main(String[] args) throws Exception {
        //文件io
        File file = new File("D:\\data\\aa\\mybatis-plus 实践及架构原理.pdf");  //发送的文件路径
        FileInputStream fis = new FileInputStream(file);

        //网络编程
        DatagramSocket socket = new DatagramSocket();
        byte[]bytes = new byte[1024];
        DatagramPacket packet;

        int len ;
        do {
            len=fis.read(bytes);
            packet = new DatagramPacket(bytes,bytes.length,InetAddress.getByName("localhost"),10000);
            socket.send(packet);
        }while (len !=-1);

        byte[] a=new byte[0];
        packet = new DatagramPacket(a,a.length,InetAddress.getByName("localhost"),10000);
        socket.send(packet);        //发送空包，代表发送完毕
        socket.close();
        System.out.println("向"+packet.getAddress().toString()+"发送文件完毕！端口号为:"+packet.getPort());
    }
}
